package ar.com.ford.it.fullEconomics.interfaces.strategys;
import java.util.Collection;
import java.util.Scanner;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.interfaces.interfaz.InterfaceProccesFile;
import ar.com.ford.it.fullEconomics.logic.PlantLogic;
import ar.com.ford.it.fullEconomics.logic.SupplierLogic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx03Intflog;
import ar.com.ford.it.fullEconomics.struct.CounterStruct;
import com.ford.it.persistence.PersistenceException;

public class SupplierStrategy implements InterfaceProccesFile {
	private static int i;
	Teess01Supplier supplier;
	public void processLine(Object aLine, String userId, Collection data) throws FullEconomicsException {
	    //use a second Scanner to parse the content of each line 
	    Scanner scanner = new Scanner((String) aLine);
	    
	    supplier = new Teess01Supplier();
	    scanner.useDelimiter(" ");
	    if ( scanner.hasNext() ){
	      i++;
	      String planta = scanner.next();
	      String GSDB = scanner.next();
	      scanner.useDelimiter("[0-9]+");
	      String name = scanner.next();
	      scanner.useDelimiter(" ");
	      //Aramar el obj etc,
	      Teesg01Plant plant = new Teesg01Plant();
	      plant.setEesg01PlantCodeC(planta);
	      try {
			supplier.setEess01PlantCodeC((new PlantLogic()).getPlant(plant.getEesg01PlantCodeC()));
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      supplier.setEess01SupplierClassCodeC("F");
	      supplier.setEess01SupplierCodeC(GSDB);
	      supplier.setEess01SupplierNameN(name);
          data.add(supplier);
	    } else {
	    	   Exception e = null;
			throw new FullEconomicsException(
	                    e,"Empty or invalid line. Unable to process");
	    }
	    //(no need for finally here, since String is source)
	    scanner.close();
	}

	public String getHeaderDescription(Object line) {
		// TODO Auto-generated method stub
		return (String) line;
	}

	public String getTrailerDescription(Object line) {
		// TODO Auto-generated method stub
		return (String) line;
	}
    
    public Integer getRecordCount(Object line){
        // TODO Auto-generated method stub
        return new Integer(0);
    }

	public boolean validateHeaderAndTrailer(Object line) {
		boolean result= true;
		line = (String) line;
		if ( line.toString().trim().length()==0 || line.toString().length()==1){
			System.out.println(line.toString().trim().length());
			result = false;
		}
			
		return result;
	}
    
    public CounterStruct insertData(Collection data, String userId, Teesx03Intflog logNumber) throws FullEconomicsException{
        SupplierLogic logic = new SupplierLogic();
        Collection<Teess01Supplier> suppliers = (Collection<Teess01Supplier>)data;
        CounterStruct counter = logic.mergeSuppliers(suppliers, userId, logNumber);
        return counter;
    }

    public boolean validateRecordCount(int trailerRecordCount, int lineCount, int dataSize) {
        return true;
    }


}
